package com.wlcj.www.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.wlcj.www.enumtype.EventTypeEnum;

import jodd.datetime.JDateTime;

public class DataUtil {
	
	public static String formatSecondStr(double sec) {
		//3天=3.5*3600
		double r1 = sec/60/60/24;  //r2>1&&r1<1显示天
		double r2 = sec/60/60;  //r3>1&&r2<1显示时
		double r3 = sec/60;  //r3<1 显示秒
		DecimalFormat df = new DecimalFormat("#.###");
		if(r1>=1) {
			return df.format(r1)+"天";
		} else if(r1<1&&r2>=1) {
			return df.format(r2)+"时";
		} else if(r2<1&&r3>=1) {
			return df.format(r3)+"分";
		} else if(r3<1) {
			return df.format(sec)+"秒";
		} 
		return null;
	}
	
	public static String formatMonthStr(int month) {
		int y = month/12;
		String ystr = y==0?"":y+"年";
		int m = month%12;
		String mstr = m==0?"":m+"月";
		return ystr+mstr;
	}
	
	public static String createTypeStr(int type) {
		JDateTime jdt = new JDateTime();
		jdt.addDay(-1);
		String yesterday = jdt.toString("YYYY-MM-DD");
		String typeStr = null;
		switch(type) {
		//昨天
		case 1:
			typeStr = yesterday+"/"+yesterday;
			break;
		//近7天
		case 2:
			typeStr = jdt.addDay(-6).toString("YYYY-MM-DD")+"/"+yesterday;
			break;
		//近30天
		case 3:
			typeStr = jdt.addDay(-29).toString("YYYY-MM-DD")+"/"+yesterday;
			break;
		}
		return typeStr;
	}
	
	/**
	 * 返回对应的事件类型id
	 * @param type
	 * @return
	 */
    public static int toProblem(String event){
    	if(event==null){
    		return EventTypeEnum.BUMING.getId();//情况不详
    	}
    	if(event.contains(EventTypeEnum.PAOLU.getDesc())){//跑路
    		return EventTypeEnum.PAOLU.getId();
    	}else if(event.contains(EventTypeEnum.TING.getDesc())){//停业
    		return EventTypeEnum.TING.getId();
    	}else if(event.contains(EventTypeEnum.TIXIAN.getDesc())){//体现困难
    		return EventTypeEnum.TIXIAN.getId();
    	}else{
    		return EventTypeEnum.BUMING.getId();//不明
    	}
    }
    
    /**问题平台条件筛选
     * @return
     */
    public static String toTime(String time){
    	StringBuilder sb = new StringBuilder();
    	if(time.contains("年全年")){
    		String year =  time.substring(0,time.indexOf("年"));
    		sb.append(year).append("-").append("1").append("-").append("1");
    	}else{
    		String year = time.substring(0,time.indexOf("年"));
        	String month = time.substring(time.indexOf("年")+1,time.indexOf("月"));
        	sb.append(year).append("-").append(month).append("-").append("1");
    	}
    	return sb.toString();
    }
    
    /**
     * 问题时间，上线时间格式化
     * @return
     */
    public static String formatTime(Date date){
    	if(date==null){
    		return "-";
    	}else{
    		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM");
    		return sd.format(date);
    	}
    }
	public static void main(String[] args) {
		formatSecondStr(1002.6d);
//		System.out.println(createTypeStr(1));
//		System.out.println(createTypeStr(2));
//		System.out.println(createTypeStr(3));
		
		
		DecimalFormat df = new DecimalFormat("#.###");
		System.out.println(df.format(0.00d));
		
		System.out.println(formatTime(new Date()));
		 Integer[] newArr = {0,1,23,5,0};
		System.out.println(removeZero(newArr).toString());
	}
	/**
	 * 数据工具类：如果数据不为空的话就取小数点后两位
	 */
	public static String detileData(BigDecimal b){
		DecimalFormat  myformart = new DecimalFormat("0.00");
		if(b!=null){
			
			return  myformart.format(b);
		}else{
			return "0";
		}
	}

	public static Integer[] removeZero(Integer[] oldArr) {
	    // 源数据组从方法参数中接收如:
	    // int[] oldArr = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 };
		if(oldArr==null){
			return null;
		}
	    // 定意新数组长度变量
	    int newLength = 0;
	    // 获得新数组长度值
	    for (Integer t : oldArr)
	        newLength += t != 0 ? 1 : 0;
	    // 定意新数组
	    Integer[] newArr = new Integer[newLength];
	    // 定意新数组下标变量
	    int n = 0;
	    // 转存数组
	    for (Integer t : oldArr) {
	        if (t != 0) {
	            newArr[n] = t;// 转存数组
	            n++;// 新数组下标偏移
	        }
	    }
	    //返回生成的新数组
	    return newArr;
	}
	public static String timeTop(String starttime){
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long diff;
		long day = 0;
		long hour = 0;
		try {
			diff = sd.parse(sd.format(new Date())).getTime() - sd.parse(starttime).getTime();
			hour = diff % nd / nh + day * 24;// 计算差多少小时
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return hour+"小时前";
	}
	
}
